#!/user/bin/env python3
# -*- coding: utf-8 -*-
"""
--------------------------------------
    Author:     JiChao_Song
    Date  :     2020/12/18 18:55
    Desc  :
--------------------------------------
"""
import jsonpath
from bson import json_util

from application import mongo


class ApiCase(mongo.Document):
    """
    api测试用例
    """

    apiNum = mongo.StringField()
    apiName = mongo.StringField(required = True)
    apiParam = mongo.ListField()
    apiPath = mongo.StringField()
    apiMethod = mongo.StringField()
    apiHeader = mongo.ListField()
    apiExtract = mongo.ListField()
    apiAssert = mongo.DictField()
    runCount = mongo.IntField(default = 1)
    response = mongo.ListField()
    isRun = mongo.BooleanField(default = True)
    isHeader = mongo.BooleanField(default = False)
    projectId = mongo.StringField()

    def set_apiParam(self):
        """
        将apiParam动态化转静态
        :return:
        """
        if len(self.apiParam) != 0 and self.apiParam:
            for i in self.apiParam:
                for j in i.items():
                    if '$' in i[j]:
                        i[j] = eval(i[j])()

        return self.apiParam

    def set_apiHeader(self):

        if len(self.apiHeader) != 0 and self.apiHeader:
            for i in self.apiHeader:
                for j in i.items():
                    if '$' in i[j]:
                        i[j] = eval(i[j])()

        return self.apiHeader

    def get_extract(self, response):
        """
        获取提取的参数
        :return:
        """
        extract = {}
        if len(self.apiExtract) != 0 and self.apiExtract:

            for i in self.apiExtract:
                extract[i] = jsonpath.jsonpath(response, i)

        return extract

    def string_apiAssert(self):

        if self.apiAssert and self.apiAssert.get('key') is None:

            return None
        else:
            return {self.apiAssert.get('key'): self.apiAssert.get('value')}

    def string_apiParam(self):
        apiParam = {}
        if self.apiParam and len(self.apiParam) > 0:
            for i in self.apiParam:
                key = i.get('key')
                value = i.get('value')
                if key:
                    item = {
                        key: value
                    }
                    apiParam.update(item)
        if len(apiParam) == 0:
            apiParam = None
        return apiParam

    def string_apiHeader(self):
        apiHeader = {}
        if self.apiHeader and len(self.apiHeader) > 0:
            for i in self.apiHeader:
                key = i.get('key')
                value = i.get('value')
                if key:
                    item = {
                        key: value
                    }
                    apiHeader.update(item)
        if len(apiHeader) == 0:
            apiHeader = None
        return apiHeader

    def to_json(self):

        return {
            '_id': eval(json_util.dumps(self.id)),
            'apiNum': self.apiNum,
            'apiName': self.apiName,
            'apiParam': self.apiParam,
            'apiMethod': self.apiMethod,
            'apiPath': self.apiPath,
            'apiHeader': self.apiHeader,
            'apiExtract': self.apiExtract,
            'apiAssert': self.apiAssert,
            'apiAssertDict': self.string_apiAssert(),
            'apiParamDict': self.string_apiParam(),
            'apiHeaderDict': self.string_apiHeader(),
            'runCount': self.runCount,
            'response': self.response,
            'isRun': self.isRun,
            'isHeader': self.isHeader,
        }

    def dict_json(self):

        return {
            'apiNum': self.apiNum,
            'apiName': self.apiName,
            'apiParam': self.apiParam,
            'apiMethod': self.apiMethod,
            'apiPath': self.apiPath,
            'apiHeader': self.apiHeader,
            'apiExtract': self.apiExtract,
            'apiAssert': self.apiAssert,
            'apiAssertDict': self.string_apiAssert(),
            'runCount': self.runCount,
            'response': self.response,
            'isRun': self.isRun,
            'isHeader': self.isHeader,
            'caseAll': 0,
            'casePass': 0,
            'caseFail': 0,
            'caseError': 0,
            'caseSkip': 0,
        }
